﻿'========================================================================
'功能:入網型態設定
'作者:andy
'日期: 2011/3/2
'========================================================================
Partial Class WebForm_Manage_NetworkTypeMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                btnInsert.Visible = False

                gvNetWorklType.Visible = False
            End If
            If Not ChkProgramRight(2) Then '新增
                btnInsert.Visible = False

            End If


            If Not ChkProgramRight(2) Then '編輯
                gvNetWorklType.Columns(0).Visible = False

                gvTypeNetWorklType.Columns(0).Visible = False
            End If


        End If

    End Sub



    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        hiKind.Value = "Insert"
        gvNetWorklType.SelectedIndex = -1
        gvNetWorklType.EditIndex = 0
        gvNetWorklType.PageIndex = 0
        gvNetWorklType.DataBind()
        gvTypeNetWorklType.Visible = False
        btnInsertDetail.Visible = False

    End Sub

    Protected Sub gvTelType_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvNetWorklType.RowCancelingEdit
        gvNetWorklType.EditIndex = -1
        hiKind.Value = ""
    End Sub

    Protected Sub gvTelType_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvNetWorklType.RowUpdated
        gvNetWorklType.EditIndex = -1
        hiKind.Value = ""
    End Sub

    Protected Sub ddlsuit_site_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlsuit_site.SelectedIndexChanged
        hiKind.Value = ""
        gvNetWorklType.SelectedIndex = -1
        gvNetWorklType.EditIndex = -1
        gvNetWorklType.DataBind()
        gvTypeNetWorklType.Visible = False '右半方資料才秀出
        btnInsertDetail.Visible = False

    End Sub

    Protected Sub gvTelType_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvNetWorklType.RowUpdating

        Dim type_code As String = CType(gvNetWorklType.Rows(e.RowIndex).FindControl("txttype_code"), TextBox).Text.Trim
        Dim type_name As String = CType(gvNetWorklType.Rows(e.RowIndex).FindControl("txttype_name"), TextBox).Text.Trim
        Dim approveLevel As Integer
        Try
            approveLevel = Integer.Parse(CType(gvNetWorklType.Rows(e.RowIndex).FindControl("txtNeedApprove"), TextBox).Text.Trim)
        Catch ex As Exception
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("NeedApproveError"))
            e.Cancel = True
            Exit Sub
        End Try
        If approveLevel < 0 Or approveLevel > 6 Then
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("NeedApproveError"))
            e.Cancel = True
            Exit Sub
        End If
        If (type_code = "") Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmpty"))
            Exit Sub
        End If
        If (type_name = "") Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmptytype_name"))
            Exit Sub
        End If
        Dim bbl As Telecom.BBL.NetWorkTypeBBL = New Telecom.BBL.NetWorkTypeBBL()
        If CType(gvNetWorklType.Rows(e.RowIndex).FindControl("txttype_code"), TextBox).Enabled Then
            If bbl.network_noIsExist(type_code, ddlsuit_site.SelectedValue, dbName) Then
                e.Cancel = True
                ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrExist"))
                Exit Sub
            End If
        End If


    End Sub

    Protected Sub gvNetWorklType_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvNetWorklType.RowCommand
        If e.CommandName = "Select" Then '選定
            gvNetWorklType.EditIndex = -1 '取消編輯模式

            Me.mpSearch.Show()
            gvTypeNetWorklType.Visible = True
            hinetwork_no.Value = e.CommandArgument.ToString()
            btnInsertDetail.Visible = ChkProgramRight(2)

        End If
        If e.CommandName = "Edit" Then
            gvTypeNetWorklType.Visible = False '右半方資料才秀出
            btnInsertDetail.Visible = False
        End If
    End Sub

    Protected Sub gvNetWorklType_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvNetWorklType.RowEditing
        gvNetWorklType.SelectedIndex = -1
        If gvNetWorklType.EditIndex <> -1 Then
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("errCancelEdit"))
            e.Cancel = True
        End If
    End Sub

    Protected Sub btnInsertDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsertDetail.Click
        Me.mpSearch.Show()
        hiKindDetail.Value = "Insert"

        gvTypeNetWorklType.SelectedIndex = -1
        gvTypeNetWorklType.EditIndex = 0
        gvTypeNetWorklType.PageIndex = 0
        gvTypeNetWorklType.DataBind()
    End Sub

    Protected Sub gvTypeNetWorklType_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvTypeNetWorklType.RowUpdating
        Me.mpSearch.Show()
        Dim typecode As String = CType(gvTypeNetWorklType.Rows(e.RowIndex).FindControl("ddlTelType"), DropDownList).SelectedValue

        If typecode = "" Then
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ApplytypecodeRequire"))
            e.Cancel = True
            Exit Sub
        End If
        Dim bbl As Telecom.BBL.NetWorkTypeBBL = New Telecom.BBL.NetWorkTypeBBL()
        If bbl.TelTypenetwork_noIsExist(hinetwork_no.Value, ddlsuit_site.SelectedValue, typecode, hiDBName.Value) Then
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ApplytypecodeExist"))
            e.Cancel = True
            Exit Sub
        End If

        hiKindDetail.Value = ""
    End Sub

    Protected Sub gvTypeNetWorklType_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvTypeNetWorklType.RowCancelingEdit
        Me.mpSearch.Show()
        hiKindDetail.Value = ""
    End Sub


    Protected Sub gvTypeNetWorklType_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvTypeNetWorklType.RowCommand

        If e.CommandName = "Delete" Then ''因為gridView不可以設datakeynames,原因為有新增時會抓到空白的欄位所以key不可設定,所以刪除要手工來處理.
            Dim typecode As String = e.CommandArgument.ToString()
            Dim bbl As Telecom.BBL.NetWorkTypeBBL = New Telecom.BBL.NetWorkTypeBBL()
            bbl.DeleteTypenetwork_no(hinetwork_no.Value, ddlsuit_site.SelectedValue, typecode, dbName)

            gvTypeNetWorklType.SelectedIndex = -1
            gvTypeNetWorklType.EditIndex = -1
            gvTypeNetWorklType.DataBind()

        End If

    End Sub

    Protected Sub gvTypeNetWorklType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvTypeNetWorklType.DataBound
        Me.mpSearch.Show()
    End Sub
End Class
